package day2;

import java.util.Random;
import java.util.Scanner;

import org.junit.Test;

public class WhileDemo {
	
	@Test
	public void test1() {
		int i =1;
//		 2、循环条件的控制
		while (i <= 5) {
			System.out.println("HelloWorld");
//			3、循环条件的改变
			i++;
			
		}
	}
	
	@Test
	public void test2() {
		for (int i = 1; i <= 5; i++) {
			System.out.println("HelloWorld");
		}
		for (;;) {
			System.out.println("HelloWorld");
		}
	}
	
	@Test
	public void test3() {
		int sum = 0;
		for (int i = 1; i <= 100; i++) {
			sum = sum + i;// sum += i;
		}
		System.out.println("sum:" + sum);
		
	}
	
	@Test
	public void test4() {
		int sum = 0;
		int i = 1;
		while (i <= 100) {
			sum = sum + i;
			i++;
		}
		System.out.println("sum:" + sum);
	}
	
	@Test
	public void test5() {
		int count =0;
		for (int i = 1; i <= 100; i++) {
			if (i % 7 == 0) {
				count++;
			}
		}
		System.out.println("能被7整除的有：" + count + "个");
	}
	
	@Test
	public void test6() {
		int product;
		for (int i = 1; i <= 9; i++) {
			//product = i * 9;
			// 豆豆加加
			System.out.print(i + "*9=" + (i * 9) + "\t");
		}
	}
	
	@Test
	public void test7() {
		// i,j,k
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
	
	@Test
	public void test8() {
		for (int i = 1; i <= 10; i++) {
			for (int j = 1; j <= 10; j++) {
				if (j == 3) {
					break;// 跳出离他最近的那个循环  continue跳过并继续
				}
				System.out.println(j);
			}
		}
	}
	
	@Test
	public void test9() {
		Scanner scanner = new Scanner(System.in);
		int random = new Random().nextInt(100) + 1;	// 0 =< random < 100 +1 ----- 1=<random<=100
		// int random = (int) (Math.random() * 100 + 1); 生成一个随机数[0,1)*100得[0,100)+1取1~100的随机数
		System.out.println(random);// parameters 参数 
		while (true) {
			System.out.println("猜吧");
			int num = scanner.nextInt();
			// 非法输入、退出操作
			if (num <= 0 || num > 100) {
				System.out.println("退出");
				break;
			}
			if (num > random) {
				System.out.println("大了");
			} else if (num < random) {
				System.out.println("小了");
			} else {
				System.out.println("猜对了");
				break;
			}
		}
	}
	
	@Test
	public void test10() {
		// 不管条件是否成功，都要执行一次
		do {
			
		} while (true);
	}
}
